﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.Security;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;
using Medianamik.UI.Web.Approbation.Data;
using Medianamik.UI.Web.MVP.Presentation;
using System.Linq;
using Telerik.Web.UI;

namespace Medianamik.UI.Web.Approbation.Presentation.Presenters
{
    public class ApprobationListPresenter<T, TView> : ModulePresenterWithGrid<T, TView,
        IApprobationRepository<T>, INodeTypeRepository, IMedianamikRepository<IMedianamikDTOWithName>>
        where TView : class, IApprobationList
        where T : class, IMedianamikDTO
    {
        public ApprobationListPresenter(TView view, IApprobationRepository<T> approbationRepository,
        INodeTypeRepository nodeTypeRepository, IMedianamikRepository<IMedianamikDTOWithName> baseTypeRepository)
            : base(view, approbationRepository, nodeTypeRepository, baseTypeRepository)
        {

        }
    }

    public class ApprobationListPresenter : ApprobationListPresenter<ApprobationDTO, IApprobationList>
    {
        protected override void Initialize(object sender, EventArgs e)
        {
            base.Initialize(sender, e);

            if (!View.IsPostBack && !View.IsPublishing)
            {
                BindExactApprobationLevel(ExactApprobationLevel);
            }
        }

        public ApprobationListPresenter(IApprobationList view, IApprobationRepository<ApprobationDTO> approbationRepository,
            INodeTypeRepository nodeTypeRepository, IMedianamikRepository<IMedianamikDTOWithName> baseTypeRepository)
            : base(view, approbationRepository, nodeTypeRepository, baseTypeRepository)
        {
        }

        protected override Core.DAL.PagedCollection<ApprobationDTO> GetData()
        {
            if (SecurityManager.ApprobationEnabled)
            {
                return Repository.GetPagedPendingApprobations(CurrentEditionCulture,
                      String.Join(",", View.MedianamikUser.Groups.Select(g => g.ID.ToString()).ToArray()), ExactApprobationLevel == 0,
                      View.IsPublishing, PageSize, PageIndex);
            }
            else
            {
                return Repository.GetPagedPendingPublications(CurrentEditionCulture,
                      View.IsPublishing, PageSize, PageIndex);
                //return NodeManager.GetModifiedNodes(MedianamikContext.Current.EditionCulture,
                //                                     MedianamikTypes.BaseType,
                //                                     MedianamikModules.ContentManagement);
            }
        }

        #region ExactApprobationLevel

        private bool _selectedExactApprobationLevelIsRetreived;
        private int? GetSelectedExactApprobationLevel()
        {
            if (View.IsPublishing || View.ExactApprobationLevel == null)
                return null;

            _selectedExactApprobationLevelIsRetreived = true;
            var selectedTheme = View.ExactApprobationLevel.FirstOrDefault(i => i.Selected) ??
                                View.ExactApprobationLevel.FirstOrDefault();

            int? exactApprobationLevel = null;

            if (selectedTheme != null && !String.IsNullOrEmpty(selectedTheme.Value))
                exactApprobationLevel = int.Parse(selectedTheme.Value);

            return exactApprobationLevel;
        }
        private int? _selectedExactApprobationLevel;
        private int? _exactApprobationLevel;
        protected virtual int? SelectedExactApprobationLevel
        {
            get
            {
                if (_selectedExactApprobationLevelIsRetreived)
                    return _selectedExactApprobationLevel;

                return _selectedExactApprobationLevel = GetSelectedExactApprobationLevel();
            }
        }
        protected virtual void BindExactApprobationLevel(int? selectedExactApprobationLevel)
        {
            var options = new[] { new ListItem(Translator.Translate("Medianamik_Approbation", "MyApprobations"), "0"), 
                new ListItem(Translator.Translate("Medianamik_Approbation", "AllAvailable"), "1") };

            if (selectedExactApprobationLevel.HasValue)
            {
                options.First(i =>
                    i.Value.Equals(selectedExactApprobationLevel.ToString())).Selected = true;
            }

            View.ExactApprobationLevel = options;
        }
        protected virtual int ExactApprobationLevel
        {
            get
            {
                return _exactApprobationLevel ?? (_exactApprobationLevel = GetExactApprobationLevel()).GetValueOrDefault(DefaultExactApprobationLevel);
            }
        }
        protected virtual int? GetExactApprobationLevel()
        {
            return View.IsPostBack ? SelectedExactApprobationLevel : 
                View.QueryString.GetValue<int?>("exact");
        }
        protected virtual int DefaultExactApprobationLevel
        {
            get
            {
                return 0;
            }
        }

        #endregion

        protected override string BuildReturnUrl()
        {
            var returlUrl = base.BuildReturnUrl();

            if (ExactApprobationLevel != DefaultExactApprobationLevel)
            {
                returlUrl = returlUrl.AddParam("exact", ExactApprobationLevel);
            }

            return returlUrl;
        }

        protected override void SetCustomGridColumns(GridItem item, 
            ApprobationDTO element, bool existInCurrentCulture)
        {
            base.SetCustomGridColumns(item, element, existInCurrentCulture);

            SetModifiedByColumn(item.FindControl("ModifiedBy") as Literal, element);
            SetModifiedOnColumn(item.FindControl("ModifiedOn") as Literal, element);
        }

        protected virtual void SetModifiedByColumn(ITextControl control, ApprobationDTO element)
        {
            control.Text = element.ModifiedBy;
        }

        protected virtual void SetModifiedOnColumn(ITextControl control, ApprobationDTO element)
        {
            control.Text = element.ModifiedOn == null ? "" : element.ModifiedOn.Value.ToString();
        }
    }
}
